package org.twndmv.ejb;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.twndmv.bl.VehicleMgmt;
import org.twndmv.blimpl.VehicleMgmtImpl;
import org.twndmv.bo.VehicleRegistration;
import org.twndmv.dao.PersonDAO;
import org.twndmv.dao.VehicleRegistrationDAO;
import org.twndmv.dto.DTOUtils;
import org.twndmv.dto.PersonDTO;
import org.twndmv.dto.VehicleRegistrationDTO;
import org.twndmv.jpa.PersonDAOImpl;
import org.twndmv.jpa.VehicleRegistrationDAOImpl;

@Stateless
@RolesAllowed({ "dmv-federated","dmv-admin","dmv-tactical" })
public class VehicleMgmtEJB implements VehicleMgmtRemote {

	private static Log log = LogFactory.getLog(VehicleMgmtEJB.class);

	@PersistenceContext(unitName = "dmvEntity")
	private EntityManager em;
	
	private VehicleMgmt carManager;
	
	private PersonDAO personDB;
	 @PostConstruct
	    public void init() {
	        try {
	            log.info("**** init ****");
	            log.info("em=" + em);
	            VehicleRegistrationDAO carDB = new VehicleRegistrationDAOImpl(em);
	            personDB = new PersonDAOImpl(em);
	            carManager = new VehicleMgmtImpl(carDB);
	        }
	        catch (Throwable ex) {
	            log.error("error in init", ex);
	            throw new EJBException("error in init" + ex);
	        }
	    }
	
	
	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	@RolesAllowed({ "dmv-tactical","dmv-admin" })
	public void addRegistration(VehicleRegistrationDTO newVehicle) {
		VehicleRegistration vehReg = DTOUtils.convertDTOToVehReg(newVehicle);
		
				
		carManager.addRegistration(vehReg);
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public List<VehicleRegistrationDTO> getRegistrationsByMakeModel(
			String make, String model) {
		List<VehicleRegistrationDTO> results = new ArrayList<VehicleRegistrationDTO>();
		
		for(VehicleRegistration car : carManager.getRegistrationsByMakeModel(make,model)) {
			results.add(DTOUtils.convertVehRegToDTO(car));
		}
		
		return results;
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public List<VehicleRegistrationDTO> getRegistrationsByOwner(PersonDTO personId) {
        List<VehicleRegistrationDTO> results = new ArrayList<VehicleRegistrationDTO>();
		
		for(VehicleRegistration car : carManager.getRegistrationsByOwner(personDB.getPersonByID(personId.getId()))) {
			results.add(DTOUtils.convertVehRegToDTO(car));
		}
		
		return results;
	}
	
	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public List<VehicleRegistrationDTO> getAllVehicles() {
List<VehicleRegistrationDTO> results = new ArrayList<VehicleRegistrationDTO>();
		
		for(VehicleRegistration car : carManager.getAllVehicles()) {
			results.add(DTOUtils.convertVehRegToDTO(car));
		}
		
		return results;
	}
	
	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public VehicleRegistrationDTO getRegistrationById(long id){
		return DTOUtils.convertVehRegToDTO(carManager.getRegistrationById(id));
	}

}
